require.config({
	baseUrl: '/SH/public/static/js/',
	paths : {
		vue: "encaptool/encvue",
		iview: "utiltool/iview/iview",
		axios: 'encaptool/encaxios',
		jquery: 'http://libs.baidu.com/jquery/2.1.1/jquery.min.js',
		BMap: 'http://api.map.baidu.com/api?v=2.0&ak=wVd85C370ZYrx34dSS0cHP5TGCT2Hnky',
		async: 'utiltool/requirejstool/requirejs-plugins/src/async'
	},
	shim: {
		vue: {  
			exports:'Vue'  
		},  
		iview: [  
			'vue'  
		],
		BMap: {
	        deps: ['jquery'],
	        exports: 'BMap'
	    }
	  }
});
require(['vue', 'iview', 'axios', 'jquery', 'async!BMap'],function(Vue, iview, axios){
	Vue.use(iview);
	new Vue({
		el: '#jsmap',
		data(){
			return {
				showMenu:false,
				model: true,
				map: null,
				point: {lon: 116.805, lat: 39.9690},
				currentpoint: null,
				destination: '三河市',
				colorArr: ['red','green','blue','pink','yellow','orange','blue','pink','yellow','orange'],
				polygonop: {strokeWeight: 2, strokeColor: "none", fillColor:'none'},
				baserequesturl: '/SH/public/index/map/',
				bunId:null,
				bunData:{
					topleft:[],
					topright:[],
					botleft:[],
					botright:[]
				},
				street: 'aaa',
				quare: null

			}
		},
		beforeMount:function(){
			this.YanJiaoBunSet('getmenuitem',this.setMenu);
		},
		mounted(){
			this.$nextTick(() => {
				this.setLayer('jsmapbg', 0);
				this.setLayer('bMap', -80);
				this.initBmap('bMap');
			});
		},
		methods: {
			setLayer: function (id, param) {
				var doc = document.documentElement;  
		        relHeight = (doc.clientHeight > doc.scrollHeight) ? doc.clientHeight : doc.scrollHeight;//获取屏幕高度和当前页面高度中的较大值  
		        document.getElementById(id).style.height = (relHeight + param)+'px';//id为bg的div就是我页面中的遮罩层
			},
			initBmap: function (id) {
				this.map = new BMap.Map(id);
				var map = this.map;
				map.enableScrollWheelZoom(true);
				//this.getBoundary(this.destination);
				//this.bunId='getYanjiaobun';
				//this.YanJiaoBunSet('getYanjiaobun',this.handleBun.bind(this));
				this.setpoint(this.point);
				var mappoint = this.setBmapCenter();
				if(!mappoint) return;
				var marker = new BMap.Marker(mappoint);  // 创建标注
				map.addOverlay(marker);               // 将标注添加到地图中
				marker.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画
			},
			YanJiaoBunSet: function (value,callback) {
				var url = this.baserequesturl + value;
				var setting = {
					method: 'GET',
					url: url
				};
				axios.call(this, callback, {setting: setting});
			},
			handleBun: function (res) {
				if(res.status === 0) {
					var rs = res.result;
					for(var i=0;i<rs.length;i++){
						if(rs[i].boundaries.length!=0){
							var overlay=this.setPolygon(rs[i], true); 
							//if(rs[i].name.street=="")
							overlay.oneSixName=rs[i].name; //1/6块的名字	
							console.log(this.bunId)		
							overlay.setFillColor('none');
							overlay.setStrokeColor('red')
							var content=rs[i].name
							this.addClickHandler(content,overlay)
						}
						if(typeof rs[i].childrenlist!="undefined"){//
							for(var j=0;j<rs[i].childrenlist.length;j++){
								var children=rs[i].childrenlist[j]; //子级轮廓 1/4内的全部建筑
								if(children.boundaries.length!=0){
									var overlay=this.setPolygon(children, true);
									overlay.oneSixName=rs[i].name; //1/6块的名字
									var content=rs[i].name
									this.addClickHandler(content,overlay)
								}

							}
						}	
						
					}
					
				}else {
					this.$Message.error(res.reason);
				}
			},
			setpoint: function (point) {
				this.currentpoint = point;
			},
			setBmapCenter: function () {
				var point = this.currentpoint;
				if(!point) return null;
				var mappoint = new BMap.Point(point.lon, point.lat); // 创建点坐标
				this.map.centerAndZoom(mappoint, 13); // 初始化地图,设置中心点坐标和地图级别
				return mappoint;
			},
			getBoundary: function (destination){       
				var bdary = new BMap.Boundary();
				var _destination = destination;
				var map = this.map;
				bdary.get(_destination, this.handleBundaryset.bind(this));
			},
			handleBundaryset: function (res) {
				this.setPolygon(res, false);
			},
			// flag 自动缩放开关
			setPolygon: function (rs, flag) {
				var polygonop = this.polygonop;
				var map = this.map;
				// map.clearOverlays();        //清除地图覆盖物       
				var count = rs.boundaries.length; //行政区域的点有多少个
				if (count === 0) {
					this.$Message.error('未能获取当前行政区域');
					return ;
				}
		        var pointArray = [];
				//for (var i = 0; i < count; i++) {
					var ply = new BMap.Polygon(rs.boundaries, polygonop); //建立多边形覆盖物
					map.addOverlay(ply);  //添加覆盖物
					ply.setFillColor(this.colorArr[parseInt(10*Math.random())])
					pointArray = pointArray.concat(ply.getPath());
					return ply;
			//	}    
				//if(flag) map.setViewport(pointArray);    //调整视野 
			},
			twolevelBunSet: function (callback, param) {
				var url = this.baserequesturl + 'gettwolevel';
				var setting = {
					method: 'GET',
					url: url
				};
				axios.call(this, callback, {data: param, setting: setting});
			},
			enterMap:function(){
				this.YanJiaoBunSet('getbulidingbun',this.handleBun.bind(this));
				this.YanJiaoBunSet('getquarezhi',this.handleBun.bind(this));//6个轮廓
				this.model=false;
				this.map.centerAndZoom(new BMap.Point(116.805,39.9690), 15); 
			},
			setMenu:function(res){
				this.quare=res.result;
				this.showMenu=true;
			},
			changeMapShow:function(a, b){
				var selection = b;
				for(var i = 0; i < b.length; i++){
					selection[i] = b[i].value;
				}
				//var selection = array([0]:leftbot, [1]:樊村 );
				if(this.map!=null){
					var allOverlays=this.map.getOverlays();
					if(selection[0]=="leftbot"){
						this.street="左下"
						for(var i=0;i<allOverlays.length;i++){
							allOverlays[i].show();
						}
					}
					if(selection[0]!=undefined&&selection[1]!=undefined){//当选择数据后再改变地图显示
					for(var i=0;i<allOverlays.length;i++){
						if(allOverlays[i].oneSixName==selection[1]){
							allOverlays[i].show();
							var point=allOverlays[i].getPath();//返回折线的点数组
							this.addClickHandler(allOverlays[i].oneSixName,allOverlays[i])
						}else if(typeof allOverlays[i].oneSixName!="undefined"){//隐藏的是我们自己添加的覆盖物，不能隐藏全部覆盖物
							allOverlays[i].hide();
							this.map.closeInfoWindow();
						}
					}
					//this.map.setViewport(over.getPath())
					this.map.panTo(point[0]);

					}
				}
			},
			addClickHandler:function(content,oval){				 
			 //   function addClickHandler(content,oval){//点击覆盖物显示信息窗口
			    	var opts = {
					width : 50,     // 信息窗口宽度
					height: 50,     // 信息窗口高度
					title : "名称" , // 信息窗口标题
					enableMessage:true//设置允许信息窗发送短息
				   };
				   
					oval.addEventListener("click",function(z){
						window.z=z;
						//openInfo(content,z)
						var pp = z.target;
						var infoWindow = new BMap.InfoWindow(content,opts);  // 创建信息窗口对象 
						var point = new BMap.Point(z.point.lng, z.point.lat);
						this.map.openInfoWindow(infoWindow,point); //开启信息窗口
						//alert(content)
					}
						
					);
			//	}
				
			}
		},
		computed: {

		}
	})
});